﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public static class IncludeExtension
    {
        /// <summary>
        /// Include扩展，根据Include条件列表使用多个Include，使用Lambda表达式。
        /// </summary>
        /// <typeparam name="TEntity">实体类型。</typeparam>
        /// <param name="dbSet">DbSet集合。</param>
        /// <param name="includes">Include条件集合，使用Lambda表达式。</param>
        /// <returns>返回Include的结果。</returns>
        public static IQueryable<TEntity> Include<TEntity>(
            this IDbSet<TEntity> dbSet,
            params Expression<Func<TEntity, object>>[] includes) where TEntity : class
        {
            IQueryable<TEntity> query = dbSet;
            foreach (var include in includes)
            {
                query = query.Include(include);
            }

            return query;
        }

        /// <summary>
        /// Include扩展，根据Include条件列表使用多个Include，使用字符串。
        /// </summary>
        /// <typeparam name="TEntity">实体类型。</typeparam>
        /// <param name="dbSet">DbSet集合。</param>
        /// <param name="includes">Include条件集合，使用字符串。</param>
        /// <returns>返回Include的结果。</returns>
        public static IQueryable<TEntity> Include<TEntity>(this IDbSet<TEntity> dbSet, params string[] includes)
            where TEntity : class
        {
            IQueryable<TEntity> query = dbSet;
            foreach (var include in includes)
            {
                query = query.Include(include);
            }

            return query;
        }
    }
}
